﻿@model CategoryWrapper
@{
    HierarchicalNodeProviderBase provider = ViewBag.Provider;
    Ajax.Dna().Widget()
        .UserPreferences(@<text>
    @{
        Ajax.Dna().Checkbox(item.GenerateFieldID("ShowAsMenu"), "Show as menu", (bool)item.UserPreferences["ShowAsMenu"]).Render();
        Ajax.Dna().TreeView(item.GenerateFieldID("TreeView"))
            .HtmlAttributes(new { style = "height:200px;border:1px solid #cccccc;overflow:auto;" })
            .Options(opts =>
            {
                opts.OnNodeSelected = "$(\"#" + item.GenerateFieldID("CategoryID") + "\").val($(node).attr(\"key\"));";
                opts.SinglePathExpand = true;
            })
            .ExpandedLevel(1)
            .SelectNode("key", item.UserPreferences["CategoryID"].ToString())
            .Bind(provider, true)
            .Render();
    }
    <input type="hidden" value="@item.GetInt("CategoryID")" id="@item.GenerateFieldID("CategoryID")" name="@item.GenerateFieldID("CategoryID")" />
    </text>)
        .Preview(widget=>{
            Ajax.Dna().TreeView(widget.GenerateFieldID("PreviewTreeView"))
                .Items(nodes=>{
                    nodes.Add("Uncategoried", "#", Url.Content("~/content/images/icon_cat_16.gif")).Items(subnodes =>
                    {
                        subnodes.Add("Category1", "#", Url.Content("~/content/images/icon_cat_16.gif"));
                        subnodes.Add("Category2", "#", Url.Content("~/content/images/icon_cat_16.gif"));
                        subnodes.Add("Category3", "#", Url.Content("~/content/images/icon_cat_16.gif"));
                        subnodes.Add("Category4", "#", Url.Content("~/content/images/icon_cat_16.gif"));
                    });
                })
                .Render();
        })
        .Content(@<text>

           @if ((Model != null) && ((IHierarchyData)Model).HasChildren)
            {
                if ((bool)item.UserPreferences["ShowAsMenu"])
                {
                    Ajax.Dna().Menu()
                        .ChangeType(MenuTypes.Vertical)
                        .DefaultStyle()
                        .Bind<CategoryWrapper>((IHierarchicalEnumerable)Model, (cat, node) =>
                        {
                            //var cat = new CategoryWrapper(c);
                            node.Text = cat.Title;
                            node.NavigateUrl = cat.PermaLink;
                            node.ImageUrl = Url.Content("~/content/images/icon_cat_16.gif");
                            node.ToolTip = cat.Description;
                            return true;
                        })
                        .Render();
                }
                else
                {
                    Ajax.Dna().TreeView()
                        .Name(item.GenerateFieldID("cat_tree"))
                        .ExpandedLevel(1)
                        .Bind<CategoryWrapper>((IHierarchicalEnumerable)Model, (cat, node) =>
                        {
                            //var cat = new CategoryWrapper(c);
                            node.Text = cat.Title + " (" + cat.TotalPosts.ToString() + ")";
                            node.NavigateUrl = cat.PermaLink.ToString();
                            node.ImageUrl = Url.Content("~/content/images/icon_cat_16.gif");
                            node.ToolTip = cat.Description;
                            return true;
                        })
                        .Render();
                }
            }
            else
            {
               <div style="padding:10px;">@Html.Global("publishing", "NoCategories")</div>
            }
        </text>)
        .Render();
}
